import {LOGIN_START,LOGIN_SUCCESS,LOGIN_FAIL} from "../action/actionTypes"
let initState={ //用户信息初始状态
  isLoading:false,
  isLogin:false,
  userInfo:null
}

let uInfo=localStorage.getItem("userInfo") //从本地存储提取用户登录信息
if(uInfo){
  initState.isLogin=true
  initState.userInfo=JSON.parse(uInfo)
}

export const userReducer=(state=initState,action)=>{
  switch(action.type){
    case LOGIN_START:
      state.isLoading = true
      return {...state}
    case LOGIN_SUCCESS:
      state.isLoading = false //关闭loading窗
      state.isLogin = true
      state.userInfo=action.payload //记录用户信息
      localStorage.setItem("userInfo",JSON.stringify(action.payload))
      return {...state}
    case LOGIN_FAIL:
      state.isLoading = false //关闭loading窗
      state.isLogin = false
      state.userInfo=null //清除用户信息
      localStorage.removeItem('userInfo') //清除本地用地信息
      return {...state}
    default:
      return state
  }
}
